package com.bw.lib_common.view

import android.os.Bundle
import android.os.Looper
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.view.BaseActivity
import com.bw.lib_base.view.BaseActivity1
import com.bw.lib_common.event.CustomEvent
import com.bw.lib_common.vm.BaseViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

abstract class BaseMvvmActivity<VDB:ViewDataBinding>:BaseActivity<VDB>(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //初始化view
        initViews()
        //初始化数据
        loadData()
        //观察数据变化
        observeDataChange()
        //注册EventBus事件总线
        EventBus.getDefault().register(this)
    }

    protected open fun initViews(){

    }

    protected open fun loadData(){

    }

    abstract fun observeDataChange()

    /**
     * EventBus接收事件
     */

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onReceiveEvent(event: CustomEvent){
        if (event!= null && event.target==this.javaClass.simpleName){
            handEvent(event)
        }
    }

    protected open fun handEvent(event: CustomEvent){

    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }


}